//
//  TrafficViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/5/10.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class TrafficViewController: BaseViewController, UITableViewDelegate,UITableViewDataSource {
    
    var segmentedControl: UISegmentedControl!
    
    var headerView: UIView!
    var tableView: UITableView!
    
    var array = NSMutableArray()
    var mark = "1"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.setNewTitle("访问量排行")
        
        //1228 - 保存到本地数据进行读取
        self.array = getLocationData(key: trafficArrayToday)
        
        InitViews()
        
    }
    
    
    func InitViews() {
        
        let array = ["今日排行","所有排行"]
        segmentedControl = UISegmentedControl.init(items: array)
        segmentedControl.frame = CGRect(x: 30, y: 74, width: screenW - 60, height: 40)
        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.tintColor = barColor
        
        
        segmentedControl.addTarget(self, action: #selector(TrafficViewController.selTab), for: UIControlEvents.valueChanged)
        self.view.addSubview(segmentedControl)
        
        tableView = UITableView.init(frame: CGRect(x: 0, y: segmentedControl.frame.maxY+10, width: screenW, height: screenH-124))
        tableView.backgroundColor = UIColor.clear
        tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
        
        setUpRefresh()
        
    }
    
    func selTab(){
        let n = segmentedControl.selectedSegmentIndex
        switch n {
        case 0:
            mark = "1"
            //1228 - 存储为本地数据
            self.array = getLocationData(key: trafficArrayToday)
            self.tableView.reloadData()
            setUpRefresh()
            break
        case 1:
            mark = "0"
            self.array = getLocationData(key: trafficArrayAll)
            self.tableView.reloadData()
            setUpRefresh()
            break
        default:
            break
        }
    }
    
    func setUpRefresh() {
        tableView.header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(TrafficViewController.refresh))
        tableView.header.beginRefreshing()
    }
    
    func refresh() {
        let user = getUserPojo()
        let requestXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><rss version=\"2.0\"><channel><t>5</t><TimeType>\(mark)</TimeType><UpperCode>\(user.UpperCode!)</UpperCode></channel></rss>"
        let url = server + superWebapp
        
        //貌似这个就是上面的 url
        HttpPostUTF(url, requestXML: requestXml) { (data, result, error) in
            if error == nil {
                
                // 1228 - 将已有数据存储在本地
                //+++++++++++++++++++++++++++++++++++
//                self.array = JsonFormt(str: result)
                //+++++++++++++++++++++++++++++++++++
                
                if self.mark == "1" {
                    //今日排行
                    setLocationData(result!, key: trafficArrayToday)
                    
                    self.array = getLocationData(key: trafficArrayToday)
                    
                }else if self.mark == "0" {
                    //所有排行
                    setLocationData(result!, key: trafficArrayAll)
                    
                    self.array = getLocationData(key: trafficArrayAll)
                }
                //+++++++++++++++++
                self.tableView.reloadData()
                self.tableView.header.endRefreshing()
            }else{
                let alert: UIAlertView = UIAlertView.init(title: "错误提示", message: error!.localizedDescription, delegate: nil, cancelButtonTitle: "知道了")
                alert.show()
                self.tableView.header.endRefreshing()
            }
        }
        
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "SchCountTableViewCellIdentifier";
        
        let cell: SchCountTableViewCell = SchCountTableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: cellIdentifier)
//        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        let dic = array[indexPath.row] as! JSON
        let schName = dic["schname"].stringValue
        let count = dic["pvnum"].stringValue
        cell.schName.text = schName
        cell.count.text = "访问：\(count)"
        
        
        return cell
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
